package de.tum.in.eist.poll.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.tum.in.eist.poll.client.PollService;
import de.tum.in.eist.poll.shared.Feedback;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.Lecturer;
import de.tum.in.eist.poll.shared.Student;
import de.tum.in.eist.poll.shared.User;

@SuppressWarnings("serial")
public class PollServiceImpl extends RemoteServiceServlet implements
		PollService {

	UserManagement userManagement = new UserManagement();
	PollDataControl pollDataControl = new PollDataControl();

	@Override
	public User login(String name, String password)
			throws IllegalArgumentException {
		User user = (User) getSessionAttribute("User");
		if (name == null && user != null) {
			return user;
		}
		if ((user = userManagement.login(name, password)) != null) {
			setSessionAttribute("User", user);
			return user;
		} else {
			return null;
		}
	}

	/**
	 * Terminate Session
	 */
	@Override
	public void logout() {
		setSessionAttribute("User", null);
	}

	@Override
	public List<Lecture> requestLectures() {
		User user = (User) getSessionAttribute("User");

		if (user instanceof Lecturer) {
			return pollDataControl.getLecturesOfLecturer((Lecturer) user);
		} else {
			return null;
		}
	}

	/**
	 * Returns a list of the dates on which the lecture has occured.
	 * 
	 * @argument lectureID Specify the lecture
	 * @return List of Dates for the specified lecture
	 */
	@Override
	public List<Date> getLectureDates(String lectureID) {
		return pollDataControl.getLectureDates(lectureID);
	}

	@Override
	public List<Feedback> requestFeedback(String lectureID, Date date) {
		User user = (User) getSessionAttribute("User");

		Lecture lecture = pollDataControl.getLecture(lectureID);
		if (user instanceof Lecturer && lecture.getLecturer().equals(user)) {
			return pollDataControl.getFeedback(lecture.getLectureID(), date);
		} else {
			return null;
		}
	}

	@Override
	public boolean submitFeedback(Feedback feedback) {
		User user = (User) getSessionAttribute("User");

		if (user instanceof Student && feedback != null) {
			pollDataControl.saveFeedback(feedback, (Student) user);
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<Lecture> getOpenLectures() {
		User user = (User) getSessionAttribute("User");
		if (user instanceof Student) {
			return pollDataControl.getOpenLectures((Student) user);
		} else {
			return new ArrayList<Lecture>();
		}
	}

	@Override
	public String registerForNewPollData(String lectureID) {
		User user = (User) getSessionAttribute("User");
		if (user instanceof Lecturer) {
			pollDataControl.waitForFeedback(lectureID);
			return lectureID;
		} else {
			return null;
		}
	}

	private Object getSessionAttribute(String key) {
		return this.getThreadLocalRequest().getSession().getAttribute(key);
	}

	private void setSessionAttribute(String key, Object value) {
		this.getThreadLocalRequest().getSession().setAttribute(key, value);
	}
}
